using Common;
using Interface;
using Model.Dto.Teacher;
using Model.Entity;
using SqlSugar;

namespace Repository;

/// <summary>
/// 教师仓储类
/// </summary>
/// <remarks>
/// 提供教师数据访问操作，包括登录验证等数据库交互功能
/// </remarks>
public class TeacherRepository
{
    private readonly ISqlSugarClient _db;
    private readonly IPasswordService  _passwordService;
    
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="db">SqlSugar数据库客户端</param>
    /// <param name="passwordService">密码服务</param>
    public TeacherRepository(ISqlSugarClient db, IPasswordService passwordService)
    {
        _db = db;
        _passwordService = passwordService;
    }
    
    /// <summary>
    /// 教师登录验证
    /// </summary>
    /// <param name="dto">教师登录数据传输对象</param>
    /// <returns>登录是否成功</returns>
    /// <exception cref="ArgumentNullException">当dto为null时抛出</exception>
    /// <exception cref="BusinessException">当用户不存在时抛出</exception>
    public async Task<bool> LoginAsync(TeacherLogin dto)
    {
        if (dto == null)
            throw new ArgumentNullException(nameof(dto), "登录信息不能为空");
            
        var dbData = await _db.Queryable<Teacher>().Where(t => t.Mobile == dto.Mobile).FirstAsync();
        if (dbData == null)
        {
            throw new BusinessException("用户不存在");
        }
        
        return _passwordService.VerifyPassword(dto.Password, dbData.Password);
    }
}